package com.itxx.keyCase.Demo3;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class UdpServer {
    public static void main(String[] args) throws IOException {
        //UDP服务器
        DatagramSocket ds = new DatagramSocket(54321);

        //创建DatagramPocket对象用于封装接收到的数据并打包
        byte[] bytes = new byte[1024];
        int length = bytes.length;
        DatagramPacket dp = new DatagramPacket(bytes, length);
        //用DatagramSocket 接受数据  阻塞方法
        ds.receive(dp);

        byte[] data = dp.getData();
        int length1 = data.length;
        System.out.println("这是通过UDP协议广播方式接受的数据:"+new String(data, 0, length1));
        ds.close();
    }
}
